package jmu.cyxwcy.librarymanagement.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import jmu.cyxwcy.librarymanagement.pojo.Book;
import jmu.cyxwcy.librarymanagement.pojo.BookDescription;
import jmu.cyxwcy.librarymanagement.pojo.Result;
import jmu.cyxwcy.librarymanagement.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/book")
@Validated
public class BookController {
    @Autowired
    private BookService bookService;

    @PostMapping("/add")
    public Result addBook(@RequestBody Map<String, Object> requestData) {
        String isbn = (String) requestData.get("isbn");
        String status = (String) requestData.get("status");
        Integer quantity;

        try {
            quantity = Integer.parseInt(requestData.get("quantity").toString());
        } catch (NumberFormatException e) {
            return Result.error("Invalid quantity format");
        }

        for (int i = 0; i < quantity; i++) {
            Book book = new Book();
            book.setIsbn(isbn);
            book.setStatus(status);
            bookService.save(book);
        }
        return Result.success();
    }

    @PutMapping("/update")
    public Result updateBook(@RequestBody Book book){
        bookService.updateById(book);
        return Result.success();
    }

    @DeleteMapping("/delete/{bookId}")
    public Result deleteBook(@PathVariable Integer bookId){
        bookService.removeById(bookId);
        return Result.success();
    }


    @GetMapping("/page")
    public Result<IPage<Book>> findPage(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize,
                                        @RequestParam(defaultValue = "") Integer bookId,
                                        @RequestParam(defaultValue = "") String status,
                                        @RequestParam(defaultValue = "") String isbn,
                                        @RequestParam(defaultValue = "") String bookName,
                                        @RequestParam(defaultValue = "") String price,
                                        @RequestParam(defaultValue = "") String description,
                                        @RequestParam(defaultValue = "") String language,
                                        @RequestParam(defaultValue = "") String shelfTime) {
        IPage<Book> page = new Page<>(pageNum, pageSize);
//      QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        MPJLambdaWrapper<Book> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(Book.class)
                .selectAs(BookDescription::getBookName, Book::getBookName)
                .selectAs(BookDescription::getDescription, Book::getDescription)
                .selectAs(BookDescription::getPrice, Book::getPrice)
                .selectAs(BookDescription::getLanguage, Book::getLanguage)
                .leftJoin(BookDescription.class, BookDescription::getIsbn, Book::getIsbn)
                .like(bookId!=null, Book::getBookId, bookId)
                .like(!price.isEmpty(), BookDescription::getPrice,price)
                .like(!status.isEmpty(), "status", status)
                .like(!description.isEmpty(), BookDescription::getDescription,description)
                .like(!isbn.isEmpty(), BookDescription::getIsbn,isbn)
                .like(!bookName.isEmpty(), BookDescription::getBookName, bookName)
                .like(!language.isEmpty(), BookDescription::getLanguage, language);
        queryWrapper.orderByAsc("book_id");
//"status"表示查找的是Book表的，如果查的不是Book表的则要写出对应表BookDescription::getIsbn,isbn。由于最后的结果要封装在<Book>
//中,所有在Book类中使用@TableField(exist = false)表示不是该表的列名
        IPage<Book> pg= bookService.join(page, queryWrapper);
        return Result.success(pg);
    }
}
